home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dosrcss.zip / RCSCLEAN.C < prev    next >
C/C++ Source or Header  |  1990-07-18  |  8KB  |  279 lines

  1. /*
  2.  *                     RCS rcsclean operation
  3.  */
  4.  
  5. static char rcsid[]=
  6. "$Header $ Purdue CS";
  7. /*****************************************************************************
  8.  *                       remove unneded working files
  9.  *****************************************************************************
  10.  */
  11.  
  12. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  13.    Distributed under license by the Free Software Foundation, Inc.
  14.  
  15. This file is part of RCS.
  16.  
  17. RCS is free software; you can redistribute it and/or modify
  18. it under the terms of the GNU General Public License as published by
  19. the Free Software Foundation; either version 1, or (at your option)
  20. any later version.
  21.  
  22. RCS is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. GNU General Public License for more details.
  26.  
  27. You should have received a copy of the GNU General Public License
  28. along with RCS; see the file COPYING.  If not, write to
  29. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  30.  
  31. Report problems and direct all questions to:
  32.  
  33.     rcs-bugs@cs.purdue.edu
  34.  
  35. */
  36.  
  37.  
  38.  
  39.  
  40. /* $Log:    rcsclean.c,v $
  41.  * Revision 5.3  90/07/15  20:24:41  lfk
  42.  * Most major fixes added between rev 5.1 and rev 5.5:
  43.  *     signals fixed so they work on MS-DOS
  44.  *     Added MKS arguments code so argv can be large
  45.  *     added code to handle slashes a'la Unix
  46.  *     added more file extensions to system from MS-DOS
  47.  * 
  48.  * Revision 5.2  90/07/15  11:32:33  ROOT_DOS
  49.  * DOS version of RCS 4.0 checked in for MODS
  50.  * by lfk@athena.mit.edu
  51.  * Also update to MSC 6.0
  52.  * 
  53.  * Revision 4.4  89/05/01  15:12:21  narten
  54.  * changed copyright header to reflect current distribution rules
  55.  * 
  56.  * Revision 4.3  88/11/08  15:59:54  narten
  57.  * removed reference to TARGETDIR
  58.  * 
  59.  * Revision 4.2  87/10/18  10:30:43  narten
  60.  * Updating version numbers. Changes relative to 1.1 are actually
  61.  * relative to 4.1
  62.  * 
  63.  * Revision 1.2  87/09/24  13:59:13  narten
  64.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  65.  * warnings)
  66.  * 
  67.  * Revision 1.1  84/01/23  14:50:16  kcs
  68.  * Initial revision
  69.  * 
  70.  * Revision 4.1  83/12/15  12:26:18  wft
  71.  * Initial revision.
  72.  * 
  73.  */
  74. #include "rcsbase.h"
  75. #define ERRCODE 2                   /*error code for exit status            */
  76. static char rcsbaseid[] = RCSBASE;
  77.  
  78. extern int    cleanup();            /* cleanup after signals                */
  79. extern char * mktempfile();         /*temporary file name generator         */
  80. extern int    fterror();            /*forward for special fatal error func. */
  81. extern struct hshentry * genrevs(); /*generate delta numbers                */
  82. extern int    nerror;               /*counter for errors                    */
  83. extern int    quietflag;            /*suppresses diagnostics                */
  84. extern FILE * finptr;               /* RCS input file                       */
  85. extern FILE * fopen();
  86.  
  87. #ifdef MSDOS
  88. extern char *gettmpdir();
  89. char tmpdir[NCPPN];
  90. #endif /* MSDOS */
  91.  
  92. char *RCSfilename;
  93. char *workfilename;
  94. char * tempfile;
  95. FILE * file1, * file2;              /*file descriptors for comparison       */
  96.  
  97.  
  98. #ifdef MKS
  99. main(int argc, char *argv[], char *env[])
  100. #else
  101. main (argc, argv)
  102. int argc;
  103. char * argv[];
  104. #endif /* MKS */
  105. {
  106.         char * cmdusage;
  107.         char command[NCPPN+revlength+40];
  108.     char * rev;                   /* revision number from command line  */
  109.         char numericrev[revlength];   /* holds expanded revision number     */
  110.     int  revnums;                 /* number of -r options               */
  111.         struct hshentry * gendeltas[hshsize];/*stores deltas to be generated*/
  112.         struct hshentry * target;
  113.         int  filecounter;
  114.     register int c1;              /* reading input                      */
  115.     int  result;                  /* result of comparison               */
  116.     int pairresult;               /* reulst of pairfilenames            */
  117. #ifdef MKS
  118.     int z = 0;
  119.     int ARGC = 0;
  120.     char **tmp;
  121.     char *env_name;
  122. #    define MAXARGS 500        /* This means 500 items on the command line */
  123.     if (!(tmp = (char **)malloc(sizeof(char *)*(MAXARGS+1)))) {
  124.         fprintf(stderr, "%s: can't allocate space for arguments\n",argv[0]);
  125.         exit(1);
  126.     }
  127.     for ( z = 0 ; env[z] != NULL; z++) {    /* loop through environment */
  128.         if (*env[z] == '~') {    /* testing for entries begining with '~' */
  129.             *++env[z];            /* increment pointer to delete '~' */
  130.             tmp[ARGC++] = env[z];    /* add it to our new array */
  131.             if (z >= MAXARGS) {
  132.                 fprintf(stderr, "%s: can't handle any more arguments\n", argv[0]);
  133.                 goto list;
  134.             }
  135.         }
  136.         else if (*env[z] == '_') {    /* testing for entries begining with _ */
  137.             *++env[z] ; *++env[z];    /* move past the '_' and the '=' */
  138.             env_name = env[z];    /* copy the name */
  139.         }
  140.     }
  141. list:
  142.     if ( STREQ( (char *) argv[0] , env_name ) ) {    /* test name against startup args */
  143.         /* environment arguments meant for this program */
  144. #    ifdef DEBUG
  145.         printf("Using shell supplied args\n");
  146. #    endif
  147.         argc = ARGC;
  148.         tmp[ARGC] = NULL;    /* the terminal NULL */
  149.         argv = tmp;
  150.     }
  151. #    ifdef DEBUG
  152.     else 
  153.         printf("Using startup supplied args\n");
  154. #    endif /* debug */
  155. #endif /* MKS */
  156.  
  157.         catchints();
  158.     cmdid = "rcsclean";
  159.     cmdusage = "command format:\n    rcsclean [-rrev] file";
  160.         filecounter=revnums=0;
  161.     quietflag=true; /* default no diagnostics */
  162.         while (--argc,++argv, argc>=1 && ((*argv)[0] == '-')) {
  163.                 switch ((*argv)[1]) {
  164.                 case 'r':
  165.         revno:  if ((*argv)[2]!='\0') {
  166.                             if (revnums==0) {
  167.                     rev= *argv+2; revnums=1;
  168.                             } else {
  169.                     fterror("too many revision numbers");
  170.                             }
  171.                         } /* do nothing for empty -r */
  172.                         break;
  173.         case 'D': /* debug option */
  174.             quietflag = false;
  175.             break;
  176.  
  177.                 default:
  178.             fterror("unknown option: %s\n%s", *argv,cmdusage);
  179.                 };
  180.         } /* end of option processing */
  181.  
  182.     if (argc<1) fterror("No input file\n%s",cmdusage);
  183.  
  184. #ifdef MSDOS
  185.     strcpy( tmpdir, gettmpdir() );
  186. #endif /* MSDOS */
  187.  
  188.         /* now handle all filenames */
  189.         do {
  190.                 finptr=NULL;
  191.         pairresult=pairfilenames(argc,argv,false,false);
  192.  
  193.         if (pairresult==0) continue; /* error */
  194.         if (!(access(workfilename,4)==0)) {
  195.             diagnose("Can't open %s",workfilename);
  196.                         continue;
  197.         } elsif (pairresult == -1) {
  198.             warn("Can't find RCS file for %s",workfilename);
  199.             continue;
  200.         }
  201.                 diagnose("RCS file: %s",RCSfilename);
  202.                 if (!trysema(RCSfilename,false)) continue; /* give up */
  203.  
  204.  
  205.                 gettree(); /* reads in the delta tree */
  206.  
  207.                 if (Head==nil) {
  208.                         error("no revisions present");
  209.                         continue;
  210.                 }
  211.                 if (revnums==0)
  212.             rev=(Dbranch!=nil?Dbranch->num:Head->num); /* default rev1 */
  213.  
  214.         if (!expandsym(rev,numericrev)) continue;
  215.                 if (!(target=genrevs(numericrev,nil,nil,nil,gendeltas))) continue;
  216.  
  217. #ifdef MSDOS
  218.         tempfile=mktempfile( tmpdir ,TMPFILE1);
  219. #else
  220.         tempfile=mktempfile("/tmp/",TMPFILE1);
  221. #endif /* MSDOS */
  222.         diagnose("retrieving revision %s",target->num);
  223.                 VOID sprintf(command,"%s -q -p%s %s > %s\n",
  224.             CO ,target->num,RCSfilename,tempfile);
  225.                 if (system(command)){
  226.                         error("co failed");
  227.                         continue;
  228.                 }
  229.         /* now do comparison */
  230.         if ((file1=fopen(tempfile,"r"))==NULL) {
  231.             error("Can't open checked out file %s",tempfile);
  232.             continue;
  233.         }
  234.         if ((file2=fopen(workfilename,"r"))==NULL) {
  235.             error("Can't open %s",workfilename);
  236.             continue;
  237.         }
  238.         result=1;
  239.         while ((c1=getc(file1))==getc(file2)) {
  240.             if (c1==EOF) {
  241.                 /* identical files; can remove working file */
  242.                 result=0;
  243.                 diagnose("files identical; %s removed",workfilename);
  244.                 if (unlink(workfilename)!=0) {
  245.                     error("Can't unlink %s",workfilename);
  246.                 }
  247.                 break;
  248.             }
  249.         }
  250.         fclose(file1); fclose(file2);
  251.  
  252.         if (result==1) diagnose ("files different");
  253.  
  254.  
  255.         } while (cleanup(),
  256.                  ++argv, --argc >=1);
  257.  
  258.  
  259.     if (nerror>0) {
  260.         exit(ERRCODE);
  261.     } else {
  262.         exit(result);
  263.     }
  264.  
  265. }
  266.  
  267.  
  268. fterror(e, e1, e2)
  269. char * e, * e1, * e2;
  270. /* prints error message and terminates program with ERRCODE */
  271. {       nerror++;
  272.         VOID fprintf(stderr,"%s error: ",cmdid);
  273.     VOID fprintf(stderr,e, e1, e2);
  274.         VOID fprintf(stderr,"\n%s aborted\n",cmdid);
  275.         VOID cleanup();
  276.     exit(ERRCODE);
  277. }
  278.  
  279.